package org.input
{
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.utils.Dictionary;

	/**
	 * 键盘记录器
	 * @author anjue
	 */
	public class KeyboardInput
	{
		private var _stage:Stage;
		
		private var _isKeyDown:Boolean = false;
		private var _keyCode:uint;
		private var _currentKeyCodeList:Vector.<uint>;
		private var _multiKeyDic:Dictionary;
		public var ascii:Array;
		
		public function KeyboardInput(stage:Stage):void
		{
			_stage = stage;
			_stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDown);
			_stage.addEventListener(KeyboardEvent.KEY_UP,keyUp);
			_stage.addEventListener(Event.DEACTIVATE,deactivate);
			_currentKeyCodeList = new Vector.<uint>();
			_multiKeyDic = new Dictionary();
			
			ascii = new Array(222);
			fillAscii();
		}
		
		private function deactivate(e:Event):void
		{
			_currentKeyCodeList = new Vector.<uint>();
			_isKeyDown = false;
		}
		
		private function keyDown(e:KeyboardEvent):void
		{
			_isKeyDown = true;
			_keyCode = e.keyCode;
			if(_currentKeyCodeList.indexOf(_keyCode) == -1)
			{
				_currentKeyCodeList.push(_keyCode);
				trace("keydown" + _keyCode);
			}
		}
		
		private function keyUp(e:KeyboardEvent):void
		{
			var index:int = _currentKeyCodeList.indexOf(e.keyCode);
			if(index != -1)
			{
				_currentKeyCodeList.splice(index,1);
				if(_currentKeyCodeList.length>0)
				{
					_keyCode = _currentKeyCodeList[_currentKeyCodeList.length-1];
				}
				trace("keyup" + e.keyCode);
			}
			if(_currentKeyCodeList.length == 0)
			{
				_isKeyDown = false;
			}
		}
		
		public function addMultiKeyEvent(keys:Vector.<uint>,fun:Function):void
		{
			
		}
		
		public function get isKeyDown():Boolean
		{
			return _isKeyDown;
		}
		
		public function get keyCode():int
		{
			return _keyCode;
		}
		
		private function fillAscii():void
		{
			ascii[65] = "A";
			ascii[66] = "B";
			ascii[67] = "C";
			ascii[68] = "D";
			ascii[69] = "E";
			ascii[70] = "F";
			ascii[71] = "G";
			ascii[72] = "H";
			ascii[73] = "I";
			ascii[74] = "J";
			ascii[75] = "K";
			ascii[76] = "L";
			ascii[77] = "M";
			ascii[78] = "N";
			ascii[79] = "O";
			ascii[80] = "P";
			ascii[81] = "Q";
			ascii[82] = "R";
			ascii[83] = "S";
			ascii[84] = "T";
			ascii[85] = "U";
			ascii[86] = "V";
			ascii[87] = "W";
			ascii[88] = "X";
			ascii[89] = "Y";
			ascii[90] = "Z";
			ascii[48] = "0";
			ascii[49] = "1";
			ascii[50] = "2";
			ascii[51] = "3";
			ascii[52] = "4";
			ascii[53] = "5";
			ascii[54] = "6";
			ascii[55] = "7";
			ascii[56] = "8";
			ascii[57] = "9";
			ascii[32] = "Spacebar";
			ascii[17] = "Ctrl";
			ascii[16] = "Shift";
			ascii[192] = "~";
			ascii[38] = "up";
			ascii[40] = "down";
			ascii[37] = "left";
			ascii[39] = "right";
			ascii[96] = "Numpad 0";
			ascii[97] = "Numpad 1";
			ascii[98] = "Numpad 2";
			ascii[99] = "Numpad 3";
			ascii[100] = "Numpad 4";
			ascii[101] = "Numpad 5";
			ascii[102] = "Numpad 6";
			ascii[103] = "Numpad 7";
			ascii[104] = "Numpad 8";
			ascii[105] = "Numpad 9";
			ascii[111] = "Numpad /";
			ascii[106] = "Numpad *";
			ascii[109] = "Numpad -";
			ascii[107] = "Numpad +";
			ascii[110] = "Numpad .";
			ascii[45] = "Insert";
			ascii[46] = "Delete";
			ascii[33] = "Page Up";
			ascii[34] = "Page Down";
			ascii[35] = "End";
			ascii[36] = "Home";
			ascii[112] = "F1";
			ascii[113] = "F2";
			ascii[114] = "F3";
			ascii[115] = "F4";
			ascii[116] = "F5";
			ascii[117] = "F6";
			ascii[118] = "F7";
			ascii[119] = "F8";
			ascii[188] = ",";
			ascii[190] = ".";
			ascii[186] = ";";
			ascii[222] = "'";
			ascii[219] = "[";
			ascii[221] = "]";
			ascii[189] = "-";
			ascii[187] = "+";
			ascii[220] = "\\";
			ascii[191] = "/";
			ascii[9] = "TAB";
			ascii[8] = "Backspace";
			//ascii[27] = "ESC";
		}
		
		public function destroy():void
		{
			_stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDown);
			_stage.removeEventListener(KeyboardEvent.KEY_UP,keyUp);
		}
	}
}